package com.qianchang.microfinance.view.base;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;

import java.lang.reflect.Type;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: <可以再自定义接口返回的任何json格式的父类>
 * 修 改 人: jinbailiang
 * 修改时间: 2017
 */

public abstract class CustomRecyclerViewActivity<T, F> extends BaseActivity implements RecyclerRefreshLayout.SuperRefreshLayoutListener, BaseRecyclerAdapter.OnItemClickListener, BaseRecyclerAdapter.OnLoadingHeaderCallBack {

    private final String TAG = this.getClass().getSimpleName();
    protected BaseRecyclerAdapter<F> mAdapter;
    protected RecyclerView mRecyclerView;
    protected RecyclerRefreshLayout mRefreshLayout;
    protected boolean isRefreshing;
    protected EmptyLayout mErrorLayout;

    protected int currentPageIndex;


    @Override
    protected void initView() {
        super.initView();
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRefreshLayout = (RecyclerRefreshLayout) findViewById(R.id.refreshLayout);
        mErrorLayout = (EmptyLayout) findViewById(R.id.error_layout);
    }


    @SuppressWarnings("unchecked")
    public void initData() {
        if (mAdapter == null) {
            mAdapter = getRecyclerAdapter();
            mAdapter.setState(BaseRecyclerAdapter.STATE_HIDE, false);
            mAdapter.setOnLoadingHeaderCallBack(this);// 必须在setAdapter之前
            mAdapter.setOnItemClickListener(this);
            mRecyclerView.setAdapter(mAdapter);
            mRefreshLayout.setSuperRefreshLayoutListener(this);
            mRecyclerView.setLayoutManager(getLayoutManager());
        }
        boolean isNeedEmptyView = isNeedEmptyView();
        if (isNeedEmptyView) {
            mRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    show();
                    onRefreshing();
                }
            });
        } else {
            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            mRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    show();
                    onRefreshing();
                }
            });
        }

        mErrorLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRefreshing();
            }
        });
    }

    public void setRefreshing(boolean refreshing) {
        isRefreshing = refreshing;
    }

    protected RequestCallBack<T> mCallBack = new RequestCallBack<T>() {

        @Override
        public Type getTokenType() {
            return getType();
        }

        @Override
        public void onSuccess(JsonResult<T> response) {
            onRequestFinish();
            handleResult(response);
            isShowEmptyView();
        }

        @Override
        public void onFailure(String msg) {
            onRequestFinish();
            onRequestError();
            isShowErrorView();
            ToastUtil.getInstant().show(CustomRecyclerViewActivity.this, msg);
        }

        @Override
        public void onConnectFailure(String msg) {
        }

    };

    protected abstract void handleResult(JsonResult<T> response);

    @Override
    public void onItemClick(int position, long itemId) {

    }

    @Override
    public void onRefreshing() {
        isRefreshing = true;;//是刷新，clear()-->addALL(list)；不是刷新（加载更多）addAll(list)
        mAdapter.setState(BaseRecyclerAdapter.STATE_HIDE, true);
        HttpParams params = new HttpParams();
        params.put("reqPageNum", 1);
        requestData(params);
    }

    @Override
    public void onLoadMore() {
        mAdapter.setState(isRefreshing ? BaseRecyclerAdapter.STATE_HIDE : BaseRecyclerAdapter.STATE_LOADING, true);
        HttpParams params = new HttpParams();
        params.put("reqPageNum", getPageIndex());
        requestData(params);
    }

    protected abstract void requestData(HttpParams params);

    protected abstract int getPageIndex();

    protected void onRequestSuccess(String code) {

    }

    protected void onRequestFinish() {
        onComplete();
    }

    protected void onRequestError() {
        if (mAdapter.getItems().size() == 0) {
            mAdapter.setState(BaseRecyclerAdapter.STATE_LOAD_ERROR, true);
        }
        if (!isRefreshing) {
            mAdapter.setState(BaseRecyclerAdapter.STATE_INVALID_NETWORK, true);
        }
    }

    protected void onComplete() {
        dismiss();
        mRefreshLayout.onComplete();
    }

    protected abstract void setListData(T t);
    /* {
        boolean hasMoreData = pageBean.getPageResult().getList() != null && pageBean.getPageResult().isHasNextPage();
        if (isRefreshing) {
            mBean.getPageResult().setList(pageBean.getPageResult().getList());
            mAdapter.clear();
            mAdapter.addAll(mBean.getPageResult().getList());
            isRefreshing = false;
        } else {
            mAdapter.addAll(pageBean.getPageResult().getList());
        }
        mRefreshLayout.setCanLoadMore(hasMoreData);
        pageIndex = isRefreshing ? 1 : pageBean.getPageResult().getPageIndex() + 1;
        if (!hasMoreData) {
            mAdapter.setState(BaseRecyclerAdapter.STATE_NO_MORE, true);
        }
    }*/

    public void isShowEmptyView() {
        if (mAdapter.getItems().size() > 0) {
            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            mRecyclerView.setVisibility(View.VISIBLE);
            mErrorLayout.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            mErrorLayout.setVisibility(View.VISIBLE);
            mErrorLayout.setErrorType(isNeedEmptyView() ? EmptyLayout.NO_DATA : EmptyLayout.HIDE_LAYOUT);
        }
    }

    public void isShowErrorView() {
        if (mAdapter.getItems().size() > 0) {
            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            mRecyclerView.setVisibility(View.VISIBLE);
            mErrorLayout.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            mErrorLayout.setVisibility(View.VISIBLE);
            mErrorLayout.setErrorType(isNeedEmptyView() ? EmptyLayout.NETWORK_ERROR : EmptyLayout.HIDE_LAYOUT);
        }
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(this);
    }

    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    protected abstract BaseRecyclerAdapter<F> getRecyclerAdapter();

    protected abstract Type getType();


    /**
     * 需要空的View
     *
     * @return isNeedEmptyView
     */
    protected boolean isNeedEmptyView() {
        return true;
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderHolder(RecyclerView.ViewHolder holder, int position) {

    }
}
